
package com.ivens.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ivens.R;
import com.ivens.beans.Actividad;
import com.ivens.beans.Categoria;
import com.ivens.common.ImageLoader;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

	public ImageLoader imageLoader;
    @Override
    public boolean areAllItemsEnabled()
    {
        return true;
    }

    private Context context;

    
    private Categoria[] categorias;

    public ExpandableListAdapter(Context context, Categoria[] categorias) {
        this.context = context;
        this.categorias = categorias;
        imageLoader=new ImageLoader(context);
        
    }

    /**
     * A general add method, that allows you to add a Vehicle to this list
     * 
     * Depending on if the category opf the vehicle is present or not,
     * the corresponding item will either be added to an existing group if it 
     * exists, else the group will be created and then the item will be added
     * @param actividad
     */
    public void addItem(Actividad actividad) {
//        if (!categorias.contains(actividad.getCategoria())) {
//        	categorias.add(actividad.getCategoria());
//        }
//        int index = categorias.indexOf(actividad.getCategoria());
        
    }

    public Object getChild(int groupPosition, int childPosition) {
        return categorias[groupPosition].getActividades()[childPosition];
    }

    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    
    // Return a child view. You can load your custom layout here.
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
        Actividad actividad = (Actividad) getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.child_layout, null);
        }
        ImageView im=(ImageView)convertView.findViewById(R.id.ChildImageView01);
        
        TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
        TextView tv1 = (TextView) convertView.findViewById(R.id.tvChild1);
        tv.setText(actividad.getTitulo());
        tv1.setText(actividad.getSubtitulo());

        // Depending upon the child type, set the imageTextView01
        //tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
       // tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.car, 0, 0, 0);
        imageLoader.DisplayImage(actividad.getImagen().toString(), im);
      
        return convertView;
    }

    public int getChildrenCount(int groupPosition) {
        return categorias[groupPosition].getActividades().length;
    }

    public Object getGroup(int groupPosition) {
        return categorias[groupPosition];
    }

    public int getGroupCount() {
        return categorias.length;
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    // Return a group view. You can load your custom layout here.
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent) {
    	Categoria categoria = (Categoria) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.group_layout, null);
        }
        ImageView im=(ImageView)convertView.findViewById(R.id.ImageView01);
        TextView tv = (TextView) convertView.findViewById(R.id.tvGroup);
        tv.setText(categoria.getNombre() +" ("+ categoria.getActividades().length +")");
        imageLoader.DisplayImage(categoria.getIcono().toString(), im);
        return convertView;
    }

    public boolean hasStableIds() {
        return true;
    }
    
    

    public boolean isChildSelectable(int arg0, int arg1) {
        return true;
    }

}
